﻿using System;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using Heyo.Controls.Animations;

namespace Heyo.Controls
{
    public class MaterialButton : ButtonBase
    {
        static MaterialButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MaterialButton),
                new FrameworkPropertyMetadata(typeof(MaterialButton)));
        }

        public MaterialButton()
        {
            AddVisualChild(_mouseWave);
        }
        public int CornerRadius
        {
            get => (int) GetValue(CornerRadiusProperty);
            set => SetValue(CornerRadiusProperty, value);
        }

        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register(
                "CornerRadius",
                typeof(int),
                typeof(MaterialButton),
                new PropertyMetadata(2)
            );

        public bool Shadow
        {
            get => (bool) GetValue(ShadowProperty);
            set
            {
                OnPropertyChanged(new DependencyPropertyChangedEventArgs(ShadowProperty, Shadow, value));
                SetValue(ShadowProperty, value);
            }
        }

        public static readonly DependencyProperty ShadowProperty =
            DependencyProperty.Register(
                "Shadow",
                typeof(bool),
                typeof(MaterialButton),
                new PropertyMetadata(true)
            );

        public bool MouseEnterAnimation
        {
            get => (bool) GetValue(MouseEnterAnimationProperty);
            set
            {
                OnPropertyChanged(new DependencyPropertyChangedEventArgs(MouseEnterAnimationProperty, Shadow, value));
                SetValue(MouseEnterAnimationProperty, value);
            }
        }

        public static readonly DependencyProperty MouseEnterAnimationProperty =
            DependencyProperty.Register(
                "MouseEnterAnimation",
                typeof(bool),
                typeof(MaterialButton),
                new PropertyMetadata(true, MouseEnterAnimationPropertyChanged)
            );

        private static void CornerRadiusPropertyChanged(DependencyObject obj,
            DependencyPropertyChangedEventArgs args)
        {
            if (obj is MaterialButton btn)
            {
                btn.CornerRadius = (int)args.NewValue;
            }
        }
        private static void WaveAnimationPropertyChanged(DependencyObject obj,
            DependencyPropertyChangedEventArgs args)
        {
            if (obj is MaterialButton btn)
            {
                btn._mouseWave.MouseDownWave = (bool)args.NewValue;
            }
        }
        private static void MouseEnterAnimationPropertyChanged(DependencyObject obj,
            DependencyPropertyChangedEventArgs args)
        {
            if (obj is MaterialButton btn)
            {
                btn._mouseWave.MouseEnterAnimation = (bool)args.NewValue;
            }
        }

        private readonly WaveClick _mouseWave=new WaveClick();

        protected override int VisualChildrenCount => 1;

        protected override Visual GetVisualChild(int index)
        {
            if (index == 0)
                return _mouseWave;

            throw new IndexOutOfRangeException();
        }

        protected override Size MeasureOverride(Size availableSize)
        {

            _mouseWave.Measure(availableSize);
            return _mouseWave.DesiredSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            var arrangeRect = new Rect()
            {
                Width = finalSize.Width,
                Height = finalSize.Height
            };

            _mouseWave.Arrange(arrangeRect);

            return finalSize;
        }
    }
}